package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DetailDailog extends JDialog {
    private JTextArea r1;
    private JTextArea r2;
    private JTextArea r3;
    private JTextArea r4;
    private JTextArea r5;
    private String r1String;
    private String r2String;
    private String r3String;
    private String r4String;
    private String r5String;
    private int dialogMode;

    public DetailDailog(JFrame frame, List<String> params, int dialogMode){
        super(frame,"详细信息",true);
        super.setLocationRelativeTo(frame);
        this.r1String = params.get(0);
        this.r2String = params.get(1);
        this.r3String = params.get(2);
        if(dialogMode == 2){
            this.r4String = params.get(3);
            this.r5String = params.get(4);
        }
        this.dialogMode = dialogMode;
        Container container = this.getContentPane();
        this.setSize(400,300);
        JPanel motherPanel = new JPanel();
        motherPanel.setLayout(new BorderLayout());
        JPanel cPanel = this.createCenterPanel();
        JPanel bPanel = this.createBottomPanel();
        motherPanel.add(cPanel,"Center");
        motherPanel.add(bPanel,"South");
        JScrollPane jScrollPane = new JScrollPane(motherPanel);
        container.add(jScrollPane,"Center");
    }

    private JPanel createCenterPanel(){
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        JLabel l1 = null,l2 = null,l3 = null,l4=null,l5=null;
        if(this.dialogMode == 1){
            l1 = new JLabel("信息记录类型代码：");
            l2 = new JLabel("信息记录类型名称：");
            l3 = new JLabel("表名：");
        }else{
            l1 = new JLabel("反馈信息代码：");
            l2 = new JLabel("反馈标签：");
            l3 = new JLabel("反馈解释：");
            l4 = new JLabel("对应的规则编码：");
            l5 = new JLabel("规则编码解释：");
        }
        this.r1 = new JTextArea();
        this.r1.setRows(2);
        this.r1.setEditable(false);
        this.r1.setLineWrap(true);
        this.r1.setText(this.r1String);
        this.r2 = new JTextArea();
        this.r2.setRows(2);
        this.r2.setEditable(false);
        this.r2.setLineWrap(true);
        this.r2.setText(this.r2String);
        this.r3 = new JTextArea();
        this.r3.setRows(2);
        this.r3.setEditable(false);
        this.r3.setLineWrap(true);
        this.r3.setText(this.r3String);
        if(this.dialogMode == 2){
            this.r4 = new JTextArea();
            this.r4.setRows(2);
            this.r4.setEditable(false);
            this.r4.setLineWrap(true);
            this.r4.setText(this.r4String);
            this.r5 = new JTextArea();
            this.r5.setRows(2);
            this.r5.setEditable(false);
            this.r5.setLineWrap(true);
            this.r5.setText(this.r5String);
        }
        GridBagConstraints leftCons = new GridBagConstraints();
        leftCons.fill = 0;
        leftCons.anchor = 13;
        leftCons.weightx = 0.0D;
        leftCons.weighty = 100.0D;
        GridBagConstraints rightCons = new GridBagConstraints();
        rightCons.fill = 2;
        rightCons.anchor = 17;
        rightCons.weightx = 100.0D;
        rightCons.weighty = 100.0D;
        this.add2Panel(centerPanel,l1,leftCons,0,0,1,1);
        this.add2Panel(centerPanel,this.r1,rightCons,1,0,3,1);
        this.add2Panel(centerPanel,l2,leftCons,0,1,1,1);
        this.add2Panel(centerPanel,this.r2,rightCons,1,1,3,1);
        this.add2Panel(centerPanel,l3,leftCons,0,2,1,1);
        this.add2Panel(centerPanel,this.r3,rightCons,1,2,3,1);
        if(dialogMode == 2){
            this.add2Panel(centerPanel,l4,leftCons,0,3,1,1);
            this.add2Panel(centerPanel,this.r4,rightCons,1,3,3,1);
            this.add2Panel(centerPanel,l5,leftCons,0,4,1,1);
            this.add2Panel(centerPanel,this.r5,rightCons,1,4,3,1);
        }
        return centerPanel;
    }

    private void add2Panel(JPanel panel,Component e,GridBagConstraints constraints,int x,int y,int w,int h){
        constraints.gridx = x;
        constraints.gridy = y;
        constraints.gridwidth = w;
        constraints.gridheight = h;
        panel.add(e,constraints);
    }

    private JPanel createBottomPanel(){
        JPanel bottomPanel = new JPanel();
        JButton confirmBtn = new JButton("确定");
        confirmBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DetailDailog.this.dispose();
            }
        });
        bottomPanel.add(confirmBtn);
        return bottomPanel;
    }
}
